package com.tianxiao.faas.common.util;

import com.tianxiao.faas.common.exception.biz.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
    private static Logger logger = LoggerFactory.getLogger(DateUtils.class);

    public static final String yyyy_M_d = "yyyy.M.d";
    public static Long format(String dateString) {
        if (dateString == null) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            // 解析字符串，转换为Date对象
            Date date = dateFormat.parse(dateString);
            return date.getTime();
        } catch (ParseException e) {
            logger.error("format-error, {} to ",dateString);
            throw new BizException("时间格式不正确，示例：2024-01-01 12:00:00");
        }
    }

    public static String format(Long l) {
        if (l == null) {
            return null;
        }
        Date date = new Date(l);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        return format;
    }

    public static String format(Long l, String format) {
        if (l == null) {
            return null;
        }
        Date date = new Date(l);
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    public static String rankName() {
        long currentTimeMillis = System.currentTimeMillis();
        String s = currentTimeMillis + "";
        return s.substring(5);
    }
}
